<template>
  <div class="app-container">

    <div style="text-align: center;margin-bottom: 1%;margin-top: 1%">
      <!--查询表单-->
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="objectQuery.nickname" placeholder="昵称"/>
        </el-form-item>

        <el-form-item>
          <el-input v-model="objectQuery.phone" placeholder="手机号"/>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
          <el-button type="default" icon="el-icon-delete" @click="resetData()">清空</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div style="text-align: center;margin-bottom: 1%;margin-top: 1%;margin-left: 2%;margin-right: 2%">
      <!-- 表格 -->
      <el-table :data="list" border stripe style="padding: 30px 0; text-align: center;margin-right: 1%;margin-left: 1%">
        <el-table-column label="头像" width="80" align="center">
          <template slot-scope="scope">
            <img :src="scope.row.avatar" width="100%">
          </template>
        </el-table-column>
        <el-table-column prop="nickname" label="昵称" width="200" align="center"/>
        <el-table-column prop="phone" label="手机号" width="250" align="center"/>
        <el-table-column prop="age" label="年龄" width="100" align="center"/>
        <el-table-column prop="gender" label="性别" width="100" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.gender === 0" type="primary">男</el-tag>
            <el-tag v-if="scope.row.gender === 1" type="warning">女</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="disabled" label="禁用状态" width="100" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.disabled === false" type="success">正常</el-tag>
            <el-tag v-if="scope.row.disabled === true" type="danger">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="administrator" label="管理员" width="100" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.administrator === false" type="danger">否</el-tag>
            <el-tag v-if="scope.row.administrator === true" type="success">是</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300" align="center">
          <template slot-scope="scope">
            <el-button type="danger" size="mini" icon="el-icon-error" @click="disabledUser(scope.row.id, true)"
                       v-if="scope.row.disabled === false">禁用
            </el-button>
            <el-button type="success" size="mini" icon="el-icon-success" @click="disabledUser(scope.row.id, false)"
                       v-if="scope.row.disabled === true">启用
            </el-button>

            <el-button type="success" size="mini" icon="el-icon-error" @click="administratorUser(scope.row.id, true)"
                       v-if="scope.row.administrator === false">设为管理员
            </el-button>
            <el-button type="danger" size="mini" icon="el-icon-success" @click="administratorUser(scope.row.id, false)"
                       v-if="scope.row.administrator === true">禁用管理员
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-pagination
        :current-page="page"
        :total="total"
        :page-size="limit"
        :page-sizes="[5, 10, 20]"
        style="padding: 30px 0; text-align: center"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="fetchData"
        @size-change="changePageSize"/>
    </div>
  </div>
</template>

<script>
  import userApi from '@/api/user'

  export default {
    // 页内数据
    data() {
      return {
        list: [],
        page: 1,
        limit: 5,
        total: 0,
        objectQuery: {}
      }
    },

    //初始化
    created() {
      this.fetchData()
    },

    methods: {
      //获取数据
      fetchData(page = 1) {
        this.page = page;
        userApi.getList(this.page, this.limit, this.objectQuery)
          .then(response => {
            this.list = response.data.rows;
            this.total = response.data.total
          })
          .catch(error => {
            console.log(error)
          })
      },

      // 改变每页记录数
      changePageSize(size) {
        this.limit = size;
        this.fetchData()
      },

      // 重置表单
      resetData() {
        this.objectQuery = {};
        this.fetchData()
      },

      // 修改禁用状态
      disabledUser(id, disabled) {
        this.objectQuery.id = id;
        this.objectQuery.disabled = disabled;
        userApi.updateUser(this.objectQuery)
          .then(response => {
            this.objectQuery = {};
            this.fetchData()
          })
          .catch(error => {
            console.log(error)
          })
      },

      // 修改管理员状态
      administratorUser(id, administrator) {
        this.objectQuery.id = id;
        this.objectQuery.administrator = administrator;
        userApi.updateUser(this.objectQuery)
          .then(response => {
            this.objectQuery = {};
            this.fetchData()
          })
          .catch(error => {
            console.log(error)
          })
      }
    }
  }
</script>
